<?php

namespace App\Exports;

use App\Models\Order;
use App\Models\Report;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;


class OrderExport implements FromCollection,WithHeadings,WithMapping
{
    protected $filters;
    /**
    * @return \Illuminate\Support\Collection
    */

    public function collection()
    {
        $query=Order::query()->with(['enterlog','user','housings']);
        if (isset($this->filters['ids'])) {
            $query->whereIn('id', explode(',',$this->filters['ids']));
        }
        return $query->get();
    }
    public function headings(): array
    {
        return [
            'OTA订单号',
            '订单来源',
            'OTA订单状态',
            '订房人姓名',
            '入住日期',
            '退房日期',
            '创建时间',
            '经营人姓名',
            '经营人电话',
            '数据更新时间',
            '数据推送时间',
            // ... 其他列标题
        ];
    }

    public function map($order): array
    {

        // 假设订单有 id 和 total 字段
        $orderData[] = [
            $order['ota_sn'],
            $order['order_source_type'],
            $order['order_type'],
            $order['name'],
            $order['enter_at']=isset($order->enterlog['enter_at']) ? $order->enterlog['enter_at'] :"",
            $order['exit_at']=isset($order->enterlog['exit_at']) ? $order->enterlog['exit_at'] :"",
            $order['created_at'],
            $order['landlord_name']=isset($order->housings['landlord_name']) ? $order->housings['landlord_name'] :"",
            $order['landlord_phone']=isset($order->housings['landlord_phone']) ? $order->housings['landlord_phone'] :"",
            "",
            "",
        ];
        // 展开订单数据，以便每行都有用户信息和对应的订单信息
        return array_merge(...$orderData);
    }
}
